package com.cskaoyan.controller.admin;

import com.cskaoyan.bean.BaseData;
import com.cskaoyan.bean.bo.CouponParam;
import com.cskaoyan.bean.bo.ListuserParam;
import com.cskaoyan.bean.pojo.MallCoupon;
import com.cskaoyan.bean.pojo.MallCouponUser;
import com.cskaoyan.bean.vo.BaseRespVo;
import com.cskaoyan.service.CouponService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;

/**
 * @author JJ
 * @description 优惠卷Controller
 * @time 2021/7/9 11:40
 */

@RestController
@RequestMapping("admin/coupon")
public class CouponController {

    @Autowired
    CouponService couponService;

    @RequestMapping("list")
    @RequiresPermissions("admin:coupon:list")
    public BaseRespVo list(CouponParam param) {

        BaseData<MallCoupon> mallCouponBaseData = couponService.listCoupon(param);

        return BaseRespVo.ok(mallCouponBaseData);
    }



    @RequestMapping("create")
    @RequiresPermissions("admin:coupon:create")
    public BaseRespVo create(@RequestBody MallCoupon param) {


        Integer i = couponService.createCoupon(param);
        if (i<=0){
            return BaseRespVo.fail();
        }

        List<MallCoupon> mallCoupons = couponService.queryCoupon(param);
        MallCoupon mallCoupon = mallCoupons.get(0);
        return BaseRespVo.ok(mallCoupon);
    }


    @RequestMapping("update")
    @RequiresPermissions("admin:coupon:update")
    public BaseRespVo update(@RequestBody MallCoupon param) {

        Integer i = couponService.updateCoupon(param);
        if (i<=0){
            return BaseRespVo.fail();
        }
        List<MallCoupon> mallCoupons = couponService.queryCoupon(param);
        MallCoupon mallCoupon = mallCoupons.get(0);

        return BaseRespVo.ok(mallCoupon);
    }


    @RequestMapping("delete")
    @RequiresPermissions("admin:coupon:delete")
    public BaseRespVo delete(@RequestBody MallCoupon param) {

        Integer i = couponService.deleteCoupon(param);
        if (i<=0){
            return BaseRespVo.fail();
        }
        return BaseRespVo.ok();
    }



    @RequestMapping("read")
    @RequiresPermissions("admin:coupon:read")
    public BaseRespVo read(Integer id) {

        MallCoupon mallCoupon = couponService.readCoupon(id);

        return BaseRespVo.ok(mallCoupon);
    }


    @RequestMapping("listuser")
    @RequiresPermissions("admin:coupon:listuser")
    public BaseRespVo listuser(ListuserParam param) {

        BaseData<MallCouponUser> mallCouponUserBaseData = couponService.listuserByCoupon(param);

        return BaseRespVo.ok(mallCouponUserBaseData);
    }


}
